package com.baihan.angus.app.game.busshow.ui.script_library.online_exhibition.waiting;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.baihan.angus.app.game.basic.base.BaseResponse;
import com.baihan.angus.app.game.basic.base.view_model.BaseViewModel;
import com.baihan.angus.app.game.basic.utils.Tooast;
import com.baihan.angus.app.game.basic.utils.net.ApiObserver;
import com.baihan.angus.app.game.common.bean.response.CreateRoomResponse;
import com.baihan.angus.app.game.common.bean.response.LoginResponse;
import com.baihan.angus.app.game.common.bean.response.RoomDetailResponse;
import com.baihan.angus.app.game.common.bean.response.RoomResponse;
import com.baihan.angus.app.game.common.configs.UserConfig;
import com.baihan.angus.app.game.common.data_center.remote.RemoteDataMng;

import java.util.Map;

public class WaitingAreaVM extends BaseViewModel {

    public WaitingAreaVM(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<RoomResponse.DataX> mldWaitingRoom = new MutableLiveData<>();
    public MutableLiveData<CreateRoomResponse> mldCreateRoom = new MutableLiveData<>();
    public MutableLiveData<RoomDetailResponse.Data> mldCreateRoomDetail = new MutableLiveData<>();
    public MutableLiveData<RoomDetailResponse.Data> mldEnterRoomDetail = new MutableLiveData<>();
    public MutableLiveData<Boolean> mldEnterResult = new MutableLiveData<>();
    public MutableLiveData<LoginResponse.Data> mldUser = new MutableLiveData<>();

    public void getWaitingAreaRoom(Map<String, Object> pMap) {
        showLoading("getWaitingAreaRoom");
        RemoteDataMng.getInstance().getRoomList(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<RoomResponse>() {
            @Override
            public void onSuccess(RoomResponse roomResponse) {
                hideLoading("getWaitingAreaRoom");
                mldWaitingRoom.setValue(roomResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("getWaitingAreaRoom");
                mldWaitingRoom.setValue(null);
            }
        });
    }

    public void createRoom(Map<String, Object> pMap) {
        showLoading("createRoom");
        RemoteDataMng.getInstance().createRoom(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<CreateRoomResponse>() {
            @Override
            public void onSuccess(CreateRoomResponse createRoomResponse) {
                hideLoading("createRoom");
                mldCreateRoom.setValue(createRoomResponse);
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("createRoom");
                mldCreateRoom.setValue(null);
            }
        });
    }

    public void getCreateRoomDetail(String sn) {
        showLoading("getCreateRoomDetail");
        RemoteDataMng.getInstance().getRoomDetail(sn, mLifecycleProvider.bindToLifecycle(), new ApiObserver<RoomDetailResponse>() {
            @Override
            public void onSuccess(RoomDetailResponse roomDetailResponse) {
                hideLoading("getCreateRoomDetail");
                mldCreateRoomDetail.setValue(roomDetailResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("getCreateRoomDetail");
                mldCreateRoomDetail.setValue(null);
            }
        });
    }

    public void enterRoom(String sn, Map<String, Object> pMap) {
        showLoading("enterRoom");
        RemoteDataMng.getInstance().enterRoom(sn, pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse baseResponse) {
                hideLoading("enterRoom");
                mldEnterResult.setValue(true);
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("enterRoom");
                mldEnterResult.setValue(false);
                Tooast.warning(msg);
            }
        });
    }

    public void getEnterRoomDetail(String sn) {
        showLoading("getEnterRoomDetail");
        RemoteDataMng.getInstance().getRoomDetail(sn, mLifecycleProvider.bindToLifecycle(), new ApiObserver<RoomDetailResponse>() {
            @Override
            public void onSuccess(RoomDetailResponse roomDetailResponse) {
                hideLoading("getEnterRoomDetail");
                mldEnterRoomDetail.setValue(roomDetailResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("getEnterRoomDetail");
                mldEnterRoomDetail.setValue(null);
            }
        });
    }

    public void getUserInfo() {
        showLoading("getUserInfo");
        RemoteDataMng.getInstance().getUserInfo( mLifecycleProvider.bindToLifecycle(), new ApiObserver<LoginResponse>() {
            @Override
            public void onSuccess(LoginResponse loginResponse) {
                hideLoading("getUserInfo");
                mldUser.setValue(loginResponse.getData());
                UserConfig.getUserConfig().saveUser(loginResponse.getData().getUserinfo());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("getUserInfo");
                mldUser.setValue(null);
                Tooast.warning(msg);
            }
        });
    }
}
